import cv2
import cv2 as cv
import numpy as np

font = cv2.FONT_HERSHEY_SIMPLEX

src = cv.imread("D:\\python\\pythonProject\\source\\img.png")
img = src.copy()

gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)

ret, binary = cv.threshold(gray, 220, 255, cv2.THRESH_BINARY)

contours, hie = cv.findContours(binary, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)

x = []
y = []

for i in range(1, 12):
    if cv.contourArea(contours[i]) > 200:
        cv.drawContours(src, contours, i, (0, 0, 255), 4)
        for j in contours[i]:
            for e in j:
                x.append(e[0])
                y.append(e[1])

        minx = min(x)
        maxx = max(x)
        miny = min(y)
        maxy = max(y)
        cv.rectangle(src, (minx, miny), (maxx, maxy), (0, 0, 0), 2)

        epsilon = 0.01 * cv.arcLength(contours[i], True)
        approx = cv.approxPolyDP(contours[i], epsilon, True)
        corners = len(approx)
        if corners == 3:
            cv.putText(src, "triangle", (minx, miny), font, 1, (0, 0, 0), 3)
        if corners == 4:
            cv.putText(src, "rectangle", (minx, miny), font, 1, (0, 0, 0), 3)
        if corners >= 5:
            cv.putText(src, "circular", (minx, miny), font, 1, (0, 0, 0), 3)

        x.clear()
        y.clear()

cv.imshow("picture", np.hstack([img, src]))

if cv.waitKey(0) & 0xFF == 27:
    cv.destroyAllWindows()
